package edu.hawaii.myisern.brown.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebLink;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
 * Tests operation of the MyIsern web pages.
 * 
 * @author Randy Cox
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class TestMainMenuActionBean {

  /** Get the test host. */
  private String testHost = (System.getProperty("test.host") == null) ? "http://localhost:8080/"
      : System.getProperty("test.host");

  /** User name to use. */
  private String username = "test";

  /** Password name to use. */
  private String password = "test";

  /**
   * Test the main menu new researcher button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuNewResearcherButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("NewResearcherLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Edit Researcher", response.getTitle());
  }

  /**
   * Test the main menu edit researcher button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuEditResearcherButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("EditResearcherLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Select Researcher", response.getTitle());
  }

  /**
   * Test the main menu list researcher button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuListResearcherButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("ListResearcherLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - List Researcher", response.getTitle());
  }

  /**
   * Test the main menu new organization button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuNewOrganizationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("NewOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Edit Organization", response.getTitle());
  }

  /**
   * Test the main menu edit organization button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuEditOrganizationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("EditOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Select Organization", response.getTitle());
  }

  /**
   * Test the main menu list organization button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuListOrganizationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("ListOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - List Organization", response.getTitle());
  }

  /**
   * Test the main menu new collaboration button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuNewCollaborationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("NewCollaborationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Edit Collaboration", response.getTitle());
  }

  /**
   * Test the main menu edit collaboration button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuEditCollaborationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("EditCollaborationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Select Collaboration", response.getTitle());
  }

  /**
   * Test the main menu list collaboration button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuListCollaborationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("ListCollaborationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - List Collaboration", response.getTitle());
  }

  /**
   * Test the main menu data integrity report button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuReportDataIntegrityButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login", response.getTitle());

    // Push login button
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(loginForm.getRequest());
    assertEquals("Checking mainMenu.jsp retrieval", "MyIsern - Main Menu", response.getTitle());

    // Push login button
    WebLink link = response.getLinkWithID("ReportDataIntegrityLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Report Data Integrity", response.getTitle());
  }

}
